define(["require", "exports", "../mve-DOM/index", "../mve/util"], function (require, exports, index_1, util_1) {
    "use strict";
    exports.__esModule = true;
    exports.menu = void 0;
    function menu(fun) {
        var body = document.body;
        var show = util_1.mve.valueOf(false);
        var top = util_1.mve.valueOf(0);
        var left = util_1.mve.valueOf(0);
        var menu = index_1.dom.root(function (me) {
            var render_object = fun(me, {});
            var p = render_object.element;
            p.type = p.type || "div";
            p.style = p.style || {};
            p.event = p.event || {};
            var style = p.style;
            var action = p.event;
            if (typeof (style) == 'function') {
                p.style = function () {
                    var s = style();
                    s.position = 'absolute';
                    s.display = show() ? "" : "none";
                    s.top = top() + "px";
                    s.left = left() + 'px';
                    return s;
                };
            }
            else {
                style.position = "absolute";
                style.display = function () {
                    return show() ? "" : "none";
                };
                style.top = function () {
                    return top() + "px";
                };
                style.left = function () {
                    return left() + "px";
                };
            }
            function hide() {
                show(false);
            }
            ;
            p.init = function (e, me) {
                var _a;
                mb.DOM.addEvent(document, "click", hide);
                var destroy = (_a = render_object.init) === null || _a === void 0 ? void 0 : _a.call(render_object, e, me);
                return function () {
                    mb.DOM.removeEvent(document, "click", hide);
                    if (destroy) {
                        destroy();
                    }
                };
            };
            return p;
        });
        var need_init = true;
        var menuInitResult;
        return {
            show: function (x, y) {
                var _a;
                if (need_init) {
                    body.appendChild(menu.element);
                    menuInitResult = (_a = menu.init) === null || _a === void 0 ? void 0 : _a.call(menu);
                    need_init = false;
                }
                left(x);
                top(y);
                show(true);
            },
            hide: function () {
                show(false);
            },
            destroy: function () {
                if (!need_init) {
                    //已经初始化过了
                    if (menuInitResult) {
                        menuInitResult();
                    }
                    body.removeChild(menu.element);
                }
            }
        };
    }
    exports.menu = menu;
});
